<?php

namespace App\Http\Controllers\Merchant;

use App\Http\Response;
use App\Thumbnail;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpWord\Exception\Exception;

class ThumbnailController extends Controller
{
    use Response;
    public function index(Request $request)
    {
        $paginate=Thumbnail::where('mch_id',auth('merchant')->id())->paginate($request->get('pageSize'),'*','pageNo');
        return $this->success(200,$paginate);
    }
    public function check(Request $request)
    {
        DB::beginTransaction();
        try {
            Thumbnail::where([['checked','=',1],['mch_id','=',auth('merchant')->id()]])->update(['checked'=>0]);
            Thumbnail::where([['id','=',$request->get('id')],['mch_id','=',auth('merchant')->id()]])->update(['checked'=>1]);
            DB::commit();
            return $this->message(200,trans('topic.update success'));

        }catch (Exception $exception){
            DB::rollBack();
            return $this->error(505,trans('topic.update failed'));
        }
    }
    public function store(Request $request)
    {
        $data=$request->all();
        $data['mch_id']=auth('merchant')->id();
        $created=Thumbnail::create($data);
        if ($created){
            return $this->message(200,trans('topic.create success'));
        }else{
            return $this->error(505,trans('topic.created failed'));
        }
    }
}
